#!/usr/bin/env bash

red="\033[0;31m"
green="\033[0;32m"
cyan="\033[0;36m"
bold="\033[1m"
nocolor="\033[0m"

source $(dirname "$0")/common.sh

#-------------------------------------------------------------------------------
function usage() {
   echo
   echo -e "Make a Kong ${yellow}alpha|beta${nocolor} release using this script:"
   echo ""
   echo "Usage:"
   if [ "$version" = "<x.y.z-alpha.n>" ]
   then
      echo "     List executed steps for a given release"
      echo "        $0 -v $version -s $1 $3"
      echo
   fi
   c=1
   step "switch"           "switch or create to the release branch"
   step "write_changelog"  "prepare the changelog"
   step "commit_changelog" "commit the changelog"
   step "update_copyright" "update copyright file"
   step "version_bump"     "bump and commit the version number"
   step "submit"           "push and submit a release PR"
   step "tag"              "tag and sign the release candidate"
   if [ "$beta" == true ]
   then
     step "docs_pr"        "push and submit a docs.konghq.com PR for the release"
     step "update_docker"  "update and submit a PR to Kong's docker-kong repo"
     step "merge_docker"   "merge, tag and sign Kong's docker-kong PR"
     step "homebrew"       "bump version and submit a PR to homebrew-kong"
     step "pongo"          "bump version and submit a PR to kong-pongo"
   fi
   exit 0
}

#-------------------------------------------------------------------------------
# Variables
#-------------------------------------------------------------------------------

version="0.0.0-alpha.0"

while getopts ':v:s:' OPTION; do
  case "$OPTION" in
    v)
      version="$OPTARG"
      ;;
    s)
      step="$OPTARG"
      ;;
    ?)
      usage
      exit 1
      ;;
  esac
done

xyzversion="${version%%-*}"
major=${xyzversion%%.*}
rest=${xyzversion#*.}
minor=${rest%%.*}
patch=${rest##*.}
prerelease=${version##*-}
rockspec="kong-$xyzversion$prerelease-0.rockspec"
branch="release/$xyzversion"
xyxversion="$major.$minor.0-(alpha|beta).n"
prev_version="$xyzversion"

beta=false
if [[ $version == *"beta"* ]]; then
  beta=true
fi

if ! [[ "$version" =~ ^[0-9]+.[0-9]+.0-(alpha|beta|rc)\.[0-9]$ ]]
then
   die "first argument must be a version in x.y.z-(alpha|beta|rc).n format"
fi

if [ "$step" = "" ]
then
   usage
fi

EDITOR="${EDITOR-$VISUAL}"

case "$step" in
   #---------------------------------------------------------------------------
   switch)
      set -e
      git pull
      git checkout "$branch" || true
      git checkout -B "$branch" || true
      git push -u origin "$branch"

      SUCCESS "Release branch is switched locally." \
              "You are ready to run the next step:" \
              "    $0 -v $version -s write_changelog"
      ;;
   #---------------------------------------------------------------------------
   write_changelog)
      if ! grep -q "\[$xyzversion\]" CHANGELOG.md
      then
         prepare_changelog
      fi

      CONFIRM "Press Enter to open your text editor ($EDITOR) to edit CHANGELOG.md" \
              "or Ctrl-C to cancel."

      $EDITOR CHANGELOG.md

      SUCCESS "If you need to further edit the changelog," \
              "you can run this step again."
              "If it is ready, you can proceed to the next step" \
              "which will commit it:" \
              "    $0 -v $version -s commit_changelog"
      ;;
   #---------------------------------------------------------------------------
   commit_changelog)
      commit_changelog "$version"

      SUCCESS "The changelog is now committed locally." \
              "You are ready to run the next step:" \
              "    $0 -v $version -s update_copyright"
      ;;

   #---------------------------------------------------------------------------
   update_copyright)
      update_copyright "$version"

      SUCCESS "The COPYRIGHT file is updated locally." \
              "You are ready to run the next step:" \
              "    $0 -v $version -s version_bump"
      ;;
   #---------------------------------------------------------------------------
   version_bump)
      sed -i.bak 's/major = [0-9]*/major = '$major'/' kong/meta.lua
      sed -i.bak 's/minor = [0-9]*/minor = '$minor'/' kong/meta.lua
      sed -i.bak 's/patch = [0-9]*/patch = '$patch'/' kong/meta.lua
      sed -i.bak 's/-- suffix.*/suffix = "'$prerelease'"/' kong/meta.lua
      sed -i.bak 's/ suffix.*/ suffix = "'$prerelease'"/' kong/meta.lua
      if ! [ -f "$rockspec" ]
      then
         old_rockspec=$(ls kong-*-0.rockspec | head -n 1)
         sed -i.bak 's/^version = ".*"/version = "'$xyzversion''$prerelease'-0"/' "$old_rockspec"
         sed -i.bak 's/^  tag = ".*"/  tag = "'$version'"/' "$old_rockspec"
      fi

      git diff kong/meta.lua $old_rockspec

      CONFIRM "If everything looks all right, press Enter to make the release commit" \
              "or Ctrl-C to cancel."

      git mv "$old_rockspec" "$rockspec"
      git add kong/meta.lua
      git add $rockspec

      git commit -m "release: $version"
      git log -n 1

      SUCCESS "Version bump for the release is now committed locally." \
              "You are ready to run the next step:" \
              "    $0 -v $version -s submit"
      ;;
   #---------------------------------------------------------------------------
   submit)
      if ! git log -n 1 | grep -q "release: $version"
      then
         die "Release commit is not at the top of the current branch. Did you commit the version bump?"
      fi

      git log

      CONFIRM "Press Enter to push the branch and open the release PR" \
              "or Ctrl-C to cancel."

      set -e
      git push --set-upstream origin "$branch"
      hub pull-request -b master -h "$branch" -m "Release: $version" -l "pr/please review,pr/do not merge" | true

      SUCCESS "Now get the above PR reviewed and approved. Before continueing on" \
              "    $0 -v $version -s tag"
      ;;
   #---------------------------------------------------------------------------
   tag)
      CONFIRM "Press Enter to tag the prerelease (it is not actually merged)" \
              "or Ctrl-C to cancel."

      set -e
      git checkout "$branch"
      git pull
      git tag -s "$version" -m "$version"
      git push origin "$version"

      make_github_release_file

      hub release create --prerelease -F release-$version.txt "$version"
      rm -f release-$version.txt

      SUCCESS "While the packages are being built continue to" \
            "     $0 -v $version -s docs_pr" \
            "After the packages are built continue to" \
            "Once they are built, you may run the following steps in parallel:" \
            "* 'update_docker', then 'merge_docker'"
            "* 'homebrew'" \
            "* 'pongo'"
      ;;
   #---------------------------------------------------------------------------
   docs_pr)
      if [ -d ../docs.konghq.com ]
      then
         cd ../docs.konghq.com
      else
         cd ..
         git clone https://github.com/kong/docs.konghq.com
         cd docs.konghq.com
      fi
      git checkout main
      git pull
      git checkout -B "$branch"
      bump_docs_kong_versions

      git diff

      CONFIRM "If everything looks all right, press Enter to commit and send a PR to https://github.com/kong/docs.konghq.com" \
              "or Ctrl-C to cancel."

      set -e
      git add app/_data/kong_versions.yml
      git commit -m "chore(*) update release metadata for $version"

      git push --set-upstream origin "$branch"
      hub pull-request -b main -h "$branch" -m "Release: $version" -l "pr/please review,pr/do not merge"

      SUCCESS "Make sure you give Team Docs a heads-up" \
              "once the release is pushed to the main repo." \
              "When the main release PR is approved, you can proceed to:" \
              "    $0 -v $version -s update_docker"
      ;;
   #---------------------------------------------------------------------------
   update_docker)
      update_docker "$version"

      SUCCESS "Make sure you get the PR above approved and merged" \
              "before continuing to the step 'merge_docker'."
      ;;
   #---------------------------------------------------------------------------
   merge_docker)
      if [ -d ../docker-kong ]
      then
         cd ../docker-kong
      else
         cd ..
         git clone https://github.com/kong/docker-kong
         cd docker-kong
      fi

      set -e
      git checkout "$branch"
      git pull
      git checkout master
      git pull
      git merge "$branch"
      git push
      git tag -s "$version" -m "$version"
      git push origin "$version"

      SUCCESS "Now you can run the next step:" \
              "    $0 -v $version -s homebrew"
      ;;
   #---------------------------------------------------------------------------
   homebrew)
      if [ -d ../homebrew-kong ]
      then
         cd ../homebrew-kong
      else
         cd ..
         git clone https://github.com/kong/homebrew-kong
         cd homebrew-kong
      fi

      git checkout master
      git pull
      git checkout -B "$branch"
      bump_homebrew

      git diff

      CONFIRM "If everything looks all right, press Enter to commit and send a PR to https://github.com/kong/homebrew-kong" \
              "or Ctrl-C to cancel."

      set -e
      git add Formula/kong.rb
      git commit -m "chore(kong) bump kong to $version"

      git push --set-upstream origin "$branch"
      hub pull-request -b master -h "$branch" -m "Release: $version"

      SUCCESS "Make sure you get the PR above approved and merged." \
              "    $0 -v $version -s pongo"
      ;;
   #---------------------------------------------------------------------------
   pongo)
      if [ -d ../kong-pongo ]
      then
         cd ../kong-pongo
      else
         cd ..
         git clone https://github.com/kong/kong-pongo
         cd kong-pongo
      fi

      git checkout master
      git pull
      ./assets/add_version.sh CE $version
      SUCCESS "Make sure you get the PR above approved and merged."
      ;;
   #---------------------------------------------------------------------------
   *)
      die "Unknown step!"
      ;;
esac

